package com.easy.controller;

import com.easy.bean.Raw_material;
import com.easy.service.RawMaterialService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/api/raw_material")
public class RawMaterialController {

    @Autowired
    private RawMaterialService rawMaterialService;

    // 新增
    @PostMapping
    public Result add(@RequestBody Raw_material rawMaterial) {
        return Result.success(rawMaterialService.add(rawMaterial));
    }

    // 编辑
    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id, @RequestBody Raw_material rawMaterial) {
        rawMaterial.setId(id);
        return Result.success(rawMaterialService.edit(rawMaterial));
    }

    // 删除
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        return Result.success("删除数据", rawMaterialService.del(id));
    }

    // 查询所有（可模糊查询）
    @GetMapping
    public Result all(
            @RequestParam(name = "name", required = false, defaultValue = "") String name
    ) {
        return Result.success(rawMaterialService.all(name));
    }

    // 查询所有（别名接口，用于处理/list请求）
    @GetMapping("/list")
    public Result list(
            @RequestParam(name = "name", required = false, defaultValue = "") String name
    ) {
        return Result.success(rawMaterialService.all(name));
    }

    // 查询单个
    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id) {
        return Result.success(rawMaterialService.one(id));
    }

    // 分页查询
    @GetMapping("/page")
    public Result page(@RequestParam(required = false, defaultValue = "1") Integer page,
                       @RequestParam(required = false, defaultValue = "10") Integer limit,
                       Raw_material rawMaterial){
        Page p = new Page();
        p.setPage(page);
        p.setLimit(limit);
        rawMaterialService.page(p, rawMaterial);
        return Result.success(p);
    }
}